<template>
	<div class="address">
<!-- //此处给组件设置key属性是为了让组件重新渲染，重新赋值value，就可以实现回填功能 -->
    <el-cascader
      clearable
      :props="props"
      :change-on-select="true"
      style="width:100%;margin-right: 20px"
      :key="modalKey"
      v-model="value"
    ></el-cascader>
    <!-- <el-button @click="changeValue">点击修改</el-button> -->
    <!-- <el-button @click="changeValue2">点击修改2</el-button> -->
    <!-- <el-button @click="changeValue3">点击修改3</el-button> -->
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      props: {
        lazy: true,
        _this:this,
        lazyLoad(node, resolve) {
          const { level } = node;
          let url =
            `/learn/class/sub?id=${this._this.pid}`;
            // `https://restapi.amap.com/v3/config/district?parameters&key=d45af4e0cfd13383d5e28b4475089f48`;
          if (level > 0) {
            url = 
            `/learn/class/sub?id=${node.value}`;
            //`https://restapi.amap.com/v3/config/district?parameters&key=d45af4e0cfd13383d5e28b4475089f48&keywords=${node.value}&subdistrict=1`;
          }
          level + 1;
          axios.get(url).then(res => {
            if(res.status == 200){
              res = res.data;
              if(res.code == 200){
                console.log(res,222);
                let nodes = res.data.map(item => {
                  return {
                    label: item.name,
                    value: item.id,
                    // leaf: level >= 3
                  };
                });
                resolve(nodes);
              }
              else{
                this._this.$message.error(res.message)
              }
              
            }
            else{
              this._this.$message.error(res.message)
            }

          });
        }
      },
      value: [],
      modalKey: 0,
    };
  },
  props: {
    pid: {
      default: 0
    }
  },
  created() {
  },
  mounted() {
    console.log(this.pid,554466);
  },
  methods: {
    //接收参数回填
    changeValue(val) {
      this.value = val;
      this.modalKey++;
      //this.$emit("addParentAge", this.parentActualAge);
    },
    //查询
    cx(){
      console.log(this.value,'childval');
      let self = this;
      let isArr = Array.isArray([this.value])
      let length = this.value.length;
      let res = '';
      if(isArr && length){
        res = this.value[this.value.length-1]
        return res;
      }
      else{
        return self.pid;
      }
    },
  }
};
</script>

<style scoped lang="scss">
.address {
  width: 100%;
}
</style>
